package com.itheima.reggie.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpSession session, @RequestBody Employee employee) {

//    查询用户

        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername());

        Employee one = employeeService.getOne(lqw);

        //    判断
        if (one == null) {
            return R.error("账户不存在");
        }

        //判断密码
        String password = employee.getPassword();
        String password1 = one.getPassword();
        byte[] bytes = password.getBytes();
        String s = DigestUtils.md5DigestAsHex(bytes);

        if (!s.equals(password1)) {
            return R.error("密码错误");
        }
        //判断账号是否禁用
        Integer status = one.getStatus();
        if (status == 0) {
            return R.error("账号禁用");
        }
        //session存储
        session.setAttribute("loginEmployee", one.getId());
        //    密码置空
        one.setPassword(null);

        //    返回数据
        return R.success(one);

    }

    @PostMapping("/logout")
    public R logout(HttpSession session) {
        //删除session
        session.removeAttribute("loginEmployee");
        return R.success("退出成功");
    }

    @PostMapping
    public R addEmployee(HttpSession session, @RequestBody Employee employee) {
        //    设置默认密码
        String s = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(s);
        /*//    设置新增时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //    设置新增人
        Long loginEmployee = (Long) session.getAttribute("loginEmployee");
        employee.setUpdateUser(loginEmployee);
        employee.setCreateUser(loginEmployee);*/

        //    调用保存数据
        boolean save = employeeService.save(employee);
        if (save) {
            log.info("保存成功");
            return R.success("新增成功");
        } else {
            log.info("保存失败");
            return R.error("保存失败");
        }

    }

    @GetMapping("/page")
    public R pageEmployee(int page, int pageSize, String name) {
        //    分页查询
        Page page1 = new Page(page, pageSize);
        //    条件过滤
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(name), Employee::getUsername, name);

        lqw.orderByAsc(Employee::getUpdateTime);

        //    数据库查询
        Page page2 = employeeService.page(page1, lqw);

        return R.success(page2);
    }

    @PutMapping
    public R updateStatus(HttpSession session, @RequestBody Employee employee) {
        /*log.info("qq{}", employee);
        employee.setUpdateTime(LocalDateTime.now());
        Long loginEmployee = (Long) session.getAttribute("loginEmployee");
        employee.setUpdateUser(loginEmployee);*/
        boolean b = employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    //根据id来查
    @GetMapping("/{id}")
    public R selectById(@PathVariable Long id) {

        Employee byId = employeeService.getById(id);
        if (byId == null) {
            return R.error("未查询到用户");
        }
        return R.success(byId);
    }

}
